<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      label-position="center"
      class="globalSerchFormStyle"
      style="margin: 10px 0px 15px 0px"
    >
      <el-form-item
        v-if="['', null].includes($store.state.user.userLevel)"
        label="油企名称"
        prop="enterpriseId"
        size="small"
        style="padding-top: 4px"
      >
        <el-select
          v-model="queryParams.enterpriseId"
          :clearable="true"
          filterable
          placeholder="请选择油企"
          style="width: 260px; margin-left: 10px"
          @change="handleSelectChange"
        >
          <el-option
            v-for="(item, index) in enterpriseList"
            :key="index"
            :value="item.enterpriseId"
            :label="item.enterpriseName"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="油站名称"
        prop="stationId"
        clearable
        size="small"
        style="padding-top: 4px"
      >
        <el-select
          v-model="queryParams.stationId"
          @change="handleChangeStation"
          :clearable="true"
          filterable
          placeholder="请选择油站"
          style="width: 200px; margin-left: 10px"
        >
          <el-option
            v-for="item in stationList"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="日期　"
        prop="createdDate"
        size="small"
        style="padding-top: 4px"
      >
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="dateRangeCreatedDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDateRangeDate"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="按班结时间段筛选 "
        prop="selectClassNoList"
        size="small"
        label-width="100px"
        style="padding-top: 4px"
      >
        <el-select
          v-model="selectClassNoList"
          multiple
          clearable
          collapse-tags
          placeholder="请选择班结时间段筛选"
          style="width: 360px; margin-left: 10px"
        >
          <el-option
            v-for="(v, index) in timeList"
            :label="v.classStartTime + '至' + v.classEndTime"
            :value="v.classNo"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="button-from"></div>
    </el-form>
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          @click="handleExport"
          size="mini"
          plain
          >导出</el-button
        ></el-col
      >
      <el-col :span="5">
          <el-button size="mini" plain @click="handleCheckTime(0)">按自然日统计</el-button>
          <el-button size="mini" plain @click="handleCheckTime(1)">按8点结算统计</el-button>
        </el-col>
    </el-row>

    <div class="summary-container">
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">消费收款金额</span>
          <span class="summary-sum">{{ workSum.allAmt }}元</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">充值收款金额</span>
          <span class="summary-sum">{{ workSum.wechatRecharge }}元</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">优惠总金额</span>
          <span class="summary-sum">{{ workSum.discountAmt }}元</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">总通道费</span>
          <span class="summary-sum">{{ workSum.scanCodeChannelFee }}元</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">到账总金额</span>
          <span class="summary-sum">{{ workSum.arrivalAmount }}元</span>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <!--
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
   -->

    <el-table
      style="width: 100%"
      :data="orderList"
      v-loading="loading"
      header-align="left"
    >
      <el-table-column label="日期" prop="date" align="center" width="180">
        <!-- 按班结时间段筛选 拼接时间 -->
        <template slot-scope="scope">
          <span v-if="scope.row.classEndTime">
            {{ scope.row.classStartTime }} <div>至 {{ scope.row.classEndTime }}</div>
          </span>
          <span v-else>
            {{ scope.row.date }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="油企" align="left">
        <el-table-column
          label="油企名称"
          prop="enterpriseName"
          align="left"
          width="120"
        ></el-table-column>
        <el-table-column
          label="油站名称"
          prop="stationName"
          align="left"
          width="120"
        ></el-table-column>
      </el-table-column>
      <el-table-column label="消费" align="left">
        <el-table-column label="微信" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.wechatConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付宝" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.alipayConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="银行卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.bankCardConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="POS机" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.posConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电子卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.electronicCardConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实体卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.physicalCardConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车队卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.fleetCardConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="现金" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.cashConsumption || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="消费赠送" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.consumptionGift || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="充值" align="left">
        <el-table-column label="移动支付" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.wechatRecharge || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付宝" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.alipayRecharge || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="现金" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.cashRecharge || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="后台" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.backstageRecharge || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充值赠送" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.rechargeGift || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="秒杀" align="left">
        <el-table-column
          label="微信"
          align="left"
          prop="saleActivityWxamt"
        ></el-table-column>
      </el-table-column>

      <el-table-column label="退款" align="left">
        <el-table-column label="移动支付" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.wechatRefund || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付宝" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.alipayRefund || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电子卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.electronicCardRefund || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实体卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.physicalCardRefund || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车队卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.fleetCardRefund || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="POS机" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.posRefund || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="积分" align="left">
        <el-table-column label="增加积分" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.addPoints || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="核销积分" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.deductPoints || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="优惠" align="left">
        <el-table-column label="立减" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.discountAmt || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优惠券" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.couponAmt || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="通道费" align="left">
        <el-table-column label="银行卡" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.bankCardChannelFee || 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="扫码" align="left">
          <template slot-scope="scope">
            <span>{{ scope.row.scanCodeChannelFee || 0 }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="到账金额" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.arrivalAmount || 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="80px"
      >
        <!--fixed="right"-->
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            disable
            @click="
              goNewPage('/overview/Overview_OperatingStatement_See', scope.row)
            "
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { timeComparison } from "@/utils/timeConversion";
import { enterpriseList } from "@/api/station/orderHistory";
import { listEnterpriseStation } from "@/api/dept";
import {
  selectBusinessDailyReportList,
  selectBusinessDailyReportSum,
  exportBusinessDailyReportList,
  selectClassTime,
} from "@/api/overview";
export default {
  name: "OperatingStatement",
  data() {
    return {
      enterpriseList: [],
      stationList: [],
      //时间间隔值
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 统计数据
      workSum: {},
      //表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      selectClassNoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseId: null,
        stationId: null,
        // 开始时间
        beginTime: null,
        // 截止时间
        endTime: null,
        checkTime: 0,
      },
      isOnlyOneStation: false, // 当前油企是否只有一个正常使用的油站
      loading: false,
      timeList: [], // 时间段筛选
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList();
    // 油企
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.queryParams.enterpriseId = this.presentEnterprise.enterpriseId;
      this.getStationList(this.queryParams.enterpriseId);
    }
    // 油站账号登录
    if (this.presentSation) {
      this.queryParams.stationId = this.presentSation;
      this.handleQuery();
    }
    this.selectTime()
  },

  methods: {
    // 点击统计结算筛选
    handleCheckTime(val) {
      this.queryParams.checkTime = val
      this.selectClassNoList = []
      this.getList()
    },
    // 筛选班结时间
    async selectTime() {
      let beginTime, endTime;
      if (this.dateRangeCreatedDate && this.dateRangeCreatedDate.length) {
        beginTime = this.dateRangeCreatedDate[0];
        endTime = this.dateRangeCreatedDate[1];
      }
      const res = await selectClassTime({
        stationId: this.queryParams.stationId || null,
        enterpriseId: this.queryParams.enterpriseId || null,
        beginTime: beginTime || null,
        endTime: endTime || null,
      });
      this.timeList = res.rows;
    },
    // 变更油站
    handleChangeStation() {
      this.selectTime();
    },
    // 选择了时间
    changeDateRangeDate() {
      this.selectTime();
    },
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },
    // 查询油站
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val, status: "1" }).then((res) => {
        this.stationList = res;
        // 判断单站，多站
        if (res.length <= 1) {
          this.isOnlyOneStation = true;
        }
        if (res.length === 1) {
          this.queryParams.stationId = res[0].stationId;
          this.selectTime();
          // this.handleQuery();
        }
      });
    },

    goNewPage(url, row) {
      this.$router.push({
        path: url,
        query: {
          routeInfo:JSON.stringify({ classEndTime: row.classEndTime,
          classStartTime: row.classStartTime,
          date: row.date,
          stationId: row.stationId,
          enterpriseId: row.enterpriseId,})
        },
      });
    },

    // 搜索列表
    getList() {
      this.loading = true;
      let queryParams = this.queryParams;
      if(this.dateRangeCreatedDate&&this.dateRangeCreatedDate.length){
          queryParams.beginTime=this.dateRangeCreatedDate[0]
          queryParams.endTime=this.dateRangeCreatedDate[1]
      }else{
         queryParams.beginTime=null
         queryParams.endTime=null
      }
      // return
      let userLevel = this.$store.state.user.userLevel;
      if (["1", "2", "5", "6"].includes(userLevel)) {
        queryParams.isDeleted = 0;
      }
        queryParams.classNoList = this.selectClassNoList.join(",");

      // queryParams.classNoList = JSON.stringify(this.selectClassNoList)
      // const time = timeComparison(
      //   30,
      //   "earching",
      //   queryParams.beginTime ? ` ${queryParams.beginTime} 00:00:00` : null,
      //   queryParams.endTime ? ` ${queryParams.endTime} 23:59:59` : null
      // );
      // console.log('222222222222222222',queryParams);
      // if (!time) return (this.loading = false);
      selectBusinessDailyReportList(queryParams)
        .then((response) => {
          this.loading = false;
          this.orderList = response.rows;
          this.total = response.total;
        })
        .catch(() => {
          this.loading = false;
          this.orderList = [];
          this.total = 0;
        });
      selectBusinessDailyReportSum(queryParams)
        .then((res) => {
          this.loading = false;
          this.workSum = {
            allAmt: res.data.allAmt === null ? 0 : res.data.allAmt, // 消费收款金额
            wechatRecharge:
              res.data.wechatRecharge === null ? 0 : res.data.wechatRecharge, // 充值收款金额
            discountAmt:
              res.data.discountAmt === null ? 0 : res.data.discountAmt, // 优惠总金额
            scanCodeChannelFee:
              res.data.scanCodeChannelFee === null
                ? 0
                : res.data.scanCodeChannelFee, // 总通道费
            arrivalAmount:
              res.data.arrivalAmount === null ? 0 : res.data.arrivalAmount, // 到账总金额
          };
        })
        .catch(() => {
          this.loading = false;
          this.workSum = {
            allAmt: 0,
            wechatRecharge: 0,
            discountAmt: 0,
            scanCodeChannelFee: 0,
            arrivalAmount: 0,
          };
        });
      this.loading = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      this.selectClassNoList = [];
      this.queryParams.checkTime = 0
      // 开始时间
      this.queryParams.beginTime = null;
      // 截止时间
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.getList();
      this.selectTime()
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;

      // const time = timeComparison(
      //   30,
      //   "export",
      //   queryParams.beginTime ? ` ${queryParams.beginTime} 00:00:00` : null,
      //   queryParams.endTime ? ` ${queryParams.endTime} 23:59:59` : null
      // );
      // if (!time) return;
      if(this.dateRangeCreatedDate&&this.dateRangeCreatedDate.length){
          queryParams.beginTime=this.dateRangeCreatedDate[0]
          queryParams.endTime=this.dateRangeCreatedDate[1]
      }else{
         queryParams.beginTime=null
         queryParams.endTime=null
      }

      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportBusinessDailyReportList(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  margin-top: -15px;
}

.button-from {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .button-style1 {
    display: inline-block;
    width: 80px;

    .el-button {
      margin: 0 0 0 15px;
    }
  }
}
::v-deep .el-table__header {
    th:not(.is-hidden):first-child {
        border-right: 1px solid #EBEEF5; // 颜色根据你表格的颜色自定义，当前颜色是element默认的颜色
    }
}
::v-deep .el-table__row {
    td:not(.is-hidden):first-child {
        // right: -1px;
        border-right: 1px solid #EBEEF5;
        border-bottom: 1px solid #EBEEF5;
    }
}
::v-deep .card-table {
  padding: 10px;
  margin-top: 0 !important;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

// 表格样式
::v-deep .el-form-item {
  margin-bottom: 0;

  .el-form-item__label {
    padding: 0;
    margin: 0;
    height: 29px;
    font-weight: 400;
    font-size: 12px;
  }

  .el-button--cyan {
    margin-center: 20px;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}

.summary-container {
  width: 100%;
  border: 1px solid #e7e3e3;
  padding: 10px 5px;
  border-radius: 10px;

  margin-bottom: 25px;
  display: flex;
  flex-direction: row;

  .summary-item {
    display: flex;
    flex-direction: column;

    border-right: 1px dashed rgba(0, 0, 0, 0.1);

    .summary-item-box {
      border-bottom: 1px dashed rgba(0, 0, 0, 0.1);
      padding: 5px;
      display: flex;
      justify-items: center;
      align-items: center;
      flex-direction: column;

      .summary-title {
        font-size: 18px;
      }

      .summary-sum {
        font-size: 24px;
        font-weight: 700;
      }
    }
  }

  .summary-item:last-child {
    border-right: none;
  }

  .summary-item-box:nth-last-child(-n + 1) {
    border-bottom: none;
  }
}

.f1 {
  flex: 1;
}
</style>
